$namespace("gdgs.ndp.gridbar");

gdgs.ndp.gridbar.showCustomerChart=function(view,gridId,config,xAxis,yAxis){
	gdgs.ndp.gridbar.showChart(view, gridId, "custom", xAxis, yAxis);
	var grid = view.get('#'+gridId);
	var ds = grid.get('dataSet')
	if(xAxis && yAxis){
		var dataType = ds.get('dataType');
		var propertyDefs = null;
		if(dataType instanceof dorado.AggregationDataType){
			 propertyDefs = dataType.get('elementDataType').get('propertyDefs');
		}else if(dataType instanceof dorado.EntityDataType){
			propertyDefs=dataType.get('propertyDefs');
		}
		gdgs.ndp.gridbar.showChart.textEditorXOnReady(propertyDefs,xAxis);
		gdgs.ndp.gridbar.showChart.textEditorYOnReady(propertyDefs,yAxis);
	}
	
	var parent = grid.$_parent;
	if(!parent){
		parent=grid.get('parent');
		grid.$_parent=parent;
	}
	var tagEditor =view.get('#tagEditorY'+gridId);
	var yAxis=view.get('#tagEditorY'+gridId+'.text');
	var xAxis=view.get('#textEditorX'+gridId+'.value');
	if(yAxis&&""!= yAxis &&xAxis&&""!=xAxis){
		var realvalueProperty="";
	 
		var $_selectEntity = tagEditor.$_selectEntity;
		if($_selectEntity){
			$_selectEntity.each(function(o){
				realvalueProperty+=o.get("name")+",";
			});
		}
		if(realvalueProperty.indexOf(',')>-1){
			realvalueProperty=realvalueProperty.substring(0,realvalueProperty.length-1);
		}
		if("" == realvalueProperty){
			realvalueProperty =yAxis;
		}
		yAxis=realvalueProperty;
	
	var chartbtn = parent;

	var series = [];
	var chart = new dorado.widget.highcharts.CustomerChart({
		'dataSet':ds,
		'chartTypeDataJSON':config.chartTypeDataJSON,
		'colorsJSON':config.colorsJSON,
		'legendDataJSON':config.legendDataJSON,
		'titleDataJSON':config.titleDataJSON,
		'specialDataJSON':config.dataSetOptionsDataJSON,
		'toolTipDataJSON':config.toolTipDataJSON,
		'xDataJSON':config.xDataJSON,
		'yDataJSON':config.yDataJSON,
		'layoutConstraint':{type:"center"},
		'xAxis':xAxis,
		'yAxis':yAxis,
		'categories':xAxis
	});
	var container =view.get('#$_container1'+gridId);
	if(chartbtn.chart){
		container.removeChild(chartbtn.chart);
	}
	chartbtn.chart=chart;
	container.addChild(chart);
	chart.doReLoad();
	}
}


gdgs.ndp.gridbar.showCustomerChartJSON=function(view,gridId,config,jsonConfig){
	var grid = view.get('#'+gridId);
	var ds = grid.get('dataSet')
	var parent = grid.$_parent;
	if(!parent){
		parent=grid.get('parent');
		grid.$_parent=parent;
	}
	var tagEditor =view.get('#tagEditorY'+gridId);
	var yAxis=view.get('#tagEditorY'+gridId+'.text');
	var xAxis=view.get('#textEditorX'+gridId+'.value');
	if(yAxis&&""!= yAxis &&xAxis&&""!=xAxis){
		var realvalueProperty="";
	 
		var $_selectEntity = tagEditor.$_selectEntity;
		if($_selectEntity){
			$_selectEntity.each(function(o){
				realvalueProperty+=o.get("name")+",";
			});
		}
		if(realvalueProperty.indexOf(',')>-1){
			realvalueProperty=realvalueProperty.substring(0,realvalueProperty.length-1);
		}
		if("" == realvalueProperty){
			realvalueProperty =yAxis;
		}
		yAxis=realvalueProperty;
	
	var chartbtn = parent;
	var series = [];
	var chart = new dorado.widget.highcharts.CustomerChart({
		'dataSet':ds,
		'chartTypeDataJSON':config.chartTypeDataJSON,
		'colorsJSON':config.colorsJSON,
		'legendDataJSON':config.legendDataJSON,
		'titleDataJSON':config.titleDataJSON,
		'specialDataJSON':config.dataSetOptionsDataJSON,
		'toolTipDataJSON':config.toolTipDataJSON,
		'xDataJSON':config.xDataJSON,
		'yDataJSON':config.yDataJSON,
		'layoutConstraint':{type:"center"},
		'xAxis':xAxis,
		'yAxis':xAxis,
		'categories':xAxis
	});
	
	var jsonObj = chart.doGetJSON();
	var jsonStr = JSON.stringify(jsonObj);
	var jsonConfigDialog = view.get('#'+jsonConfig);
	view.get("#textAreaJSON"+gridId).set("text",jsonStr);
	jsonConfigDialog.show();
	var container =view.get('#$_container1'+gridId);
	if(chartbtn.chart){
		container.removeChild(chartbtn.chart);
	}
	chartbtn.chart=chart;
	container.addChild(chart);
	}
}


gdgs.ndp.gridbar.showCustomerJSONChart=function(view,json,gridId){

	var chart = json.chart;
	//初始化对颜色处理
	var backgroundColor = chart['backgroundColor'];
	if(backgroundColor instanceof Object){
		if(backgroundColor['linearGradient']){
			chart['jblx']='1';
		}else if(backgroundColor['radialGradient']){
			chart['jblx']='2';
		}
		var stops = backgroundColor['stops'];
		chart['fColor']=stops[0][1];
		chart['sColor']=stops[1][1];
	}
	var plotBackgroundColor =  chart['plotBackgroundColor'];
	if(plotBackgroundColor instanceof Object){
		if(plotBackgroundColor['linearGradient']){
			chart['plotjblx']='1';
		}else if(plotBackgroundColor['radialGradient']){
			chart['plotjblx']='2';
		}
		var stops = plotBackgroundColor['stops'];
		chart['plotfColor']=stops[0][1];
		chart['plotsColor']=stops[1][1];
	}
	//end
	var legend = json.legend;
	//legend
	if(legend){
		var legendBackgroundColor =  legend['backgroundColor'];
		if(legendBackgroundColor instanceof Object){
			if(legendBackgroundColor['linearGradient']){
				legend['legendjblx']='1';
			}else if(legendBackgroundColor['radialGradient']){
				legend['legendjblx']='2';
			}
			var stops = legendBackgroundColor['stops'];
			legend['legendfColor']=stops[0][1];
			legend['legendsColor']=stops[1][1];
		}
	}
	
	var title = json.title;
	var plotOptions = json.plotOptions;
	var tooltip = json.tooltip;
	if(tooltip){
		var tooltipBackgroundColor =  tooltip['backgroundColor'];
		if(tooltipBackgroundColor instanceof Object){
			if(tooltipBackgroundColor['linearGradient']){
				tooltip['tooltipjblx']='1';
			}else if(tooltipBackgroundColor['radialGradient']){
				tooltip['tooltipjblx']='2';
			}
			var stops = tooltipBackgroundColor['stops'];
			tooltip['tooltipfColor']=stops[0][1];
			tooltip['tooltipsColor']=stops[1][1];
		}
	}
	
	var xAxis = json.xAxis;
	if(xAxis){
		var alternateGridXColor =  xAxis['alternateGridColor'];
		if(alternateGridXColor instanceof Object){
			if(alternateGridXColor['linearGradient']){
				xAxis['alternateGridjblx']='1';
			}else if(alternateGridXColor['radialGradient']){
				xAxis['alternateGridjblx']='2';
			}
			var stops = alternateGridXColor['stops'];
			xAxis['alternateGridfColor']=stops[0][1];
			xAxis['alternateGridsColor']=stops[1][1];
		}
		var xtitle =  xAxis['title'];
		if(xtitle){
			xAxis['title'] =  xtitle['text'];
		}
	}
	
	
	var yAxis = json.yAxis;
	if(yAxis){
		var alternateGridYColor =  yAxis['alternateGridColor'];
		if(alternateGridYColor instanceof Object){
			if(alternateGridYColor['linearGradient']){
				yAxis['alternateGridjblx']='1';
			}else if(alternateGridYColor['radialGradient']){
				yAxis['alternateGridjblx']='2';
			}
			var stops = alternateGridYColor['stops'];
			yAxis['alternateGridfColor']=stops[0][1];
			yAxis['alternateGridsColor']=stops[1][1];
		}
		var ytitle =  yAxis['title'];
		if(ytitle){
			yAxis['title'] =  ytitle['text'];
		}
	}

	view.get("#dataSetChartType"+gridId+"").clear();
	view.get("#dataSetLegend"+gridId+"").clear();
	view.get("#dataSetTitle"+gridId+"").clear();
	view.get("#dataSetPlot"+gridId+"").clear();
	view.get("#dataSetSpecial"+gridId+"").clear();
	view.get("#dataSetToolTip"+gridId+"").clear();
	view.get("#dataSetX"+gridId+"").clear();
	view.get("#dataSetY"+gridId+"").clear();
	view.get("#dataSetOptions"+gridId+"").clear();
	view.get("#dataSetDataLabels"+gridId+"").clear();
	
	var specialData;
	if(plotOptions){
		 specialData = plotOptions[chart.type];
	}
	var plotData;
	var datalabels;
	if(specialData){
		var zoomType = chart["zoomType"];
		var grouping = specialData["grouping"];
		var stacking = specialData["stacking"];
		var colorByPoint = specialData["colorByPoint"];
		var allowPointSelect = specialData["allowPointSelect"];
		var shadow = specialData["shadow"];
		var negativeFillColor = specialData["negativeFillColor"];
		var fillColor = specialData["fillColor"];
		var negativeColor = specialData["negativeColor"];
		var borderColor = specialData["borderColor"];
		
		if(zoomType){
			plotData["zoomType"]=zoomType;
		}
		if(grouping){
			plotData["grouping"]=grouping;
		}
		if(stacking){
			plotData["stacking"]=stacking;
		}
		if(colorByPoint){
			plotData["colorByPoint"]=colorByPoint;
		}
		if(allowPointSelect){
			plotData["allowPointSelect"]=allowPointSelect;
		}
		if(shadow){
			plotData["shadow"]=shadow;
		}
		if(negativeFillColor){
			plotData["negativeFillColor"]=negativeFillColor;
		}
		if(fillColor){
			plotData["fillColor"]=fillColor;
		}
		if(negativeColor){
			plotData["negativeColor"]=negativeColor;
		}
		if(borderColor){
			plotData["borderColor"]=borderColor;
		}
		 datalabels = specialData["dataLabels"];
	}
	view.get("#dataSetDataLabels"+gridId+"").insert(datalabels);
	view.get("#dataSetChartType"+gridId+"").insert(chart);
	view.get("#dataSetLegend"+gridId+"").insert(legend);
	view.get("#dataSetTitle"+gridId+"").insert(title);
	view.get("#dataSetPlot"+gridId+"").insert(plotData);
	view.get("#dataSetSpecial"+gridId+"").insert(specialData);
	view.get("#dataSetToolTip"+gridId+"").insert(tooltip);
	view.get("#dataSetX"+gridId+"").insert(xAxis);
	view.get("#dataSetY"+gridId+"").insert(yAxis);
	view.get("#dataSetOptions"+gridId+"").insert();

}

//设置颜色
gdgs.ndp.gridbar.valueColor = function(jblx,fColor,sColor){
	var color ;
	switch(jblx){
		case '0':
			color = fColor;
		break;
		case '1':
			color = {
				linearGradient:{x1:0,x2:0,y1:0,y1:1},
				stops:[
					[0,fColor],
					[1,sColor]
				]
			}
		break;
		case '2':
			color = {
				radialGradient:{cx:0.5,cy:0.5,r:0.5},
				stops:[
					[0,fColor],
					[1,sColor]
				]
			}
		break;
	}
	return color;
}
//提取config信息
gdgs.ndp.gridbar.generateConfig=function(dateSetDataConfig){
	var chartTypeData = dateSetDataConfig.chartTypeData;
	var legendData = dateSetDataConfig.legendData;
	var titleData = dateSetDataConfig.titleData;
	var plotData = dateSetDataConfig.plotData;
	var specialData = dateSetDataConfig.specialData;
	var toolTipData = dateSetDataConfig.toolTipData;
	var xData = dateSetDataConfig.xData;
	var yData = dateSetDataConfig.yData;
	var dataLabelsData = dateSetDataConfig.dataLabelsData;
	var optionsData = dateSetDataConfig.optionsData;
	var colorsData = dateSetDataConfig.colorsData;
	
	var chartTypeDataJSON = gdgs.ndp.gridbar.generateChartType(chartTypeData,plotData);
	var colorsJSON = gdgs.ndp.gridbar.generateColors(colorsData);
	var legendDataJSON = gdgs.ndp.gridbar.generateLegend(legendData);
	var titleDataJSON = gdgs.ndp.gridbar.generateTitle(titleData);
	var dataSetOptionsDataJSON = gdgs.ndp.gridbar.generateOptions(optionsData,chartTypeData,plotData,specialData,dataLabelsData);
	var toolTipDataJSON = gdgs.ndp.gridbar.generateToolTip(toolTipData);
	var xDataJSON = gdgs.ndp.gridbar.generateXData(xData);
	var yDataJSON = gdgs.ndp.gridbar.generateYData(yData);
	var config ={
			'chartTypeDataJSON':chartTypeDataJSON,
			'colorsJSON':colorsJSON,
			'legendDataJSON':legendDataJSON,
			'titleDataJSON':titleDataJSON,
			'dataSetOptionsDataJSON':dataSetOptionsDataJSON,
			'toolTipDataJSON':toolTipDataJSON,
			'xDataJSON':xDataJSON,
			'yDataJSON':yDataJSON
		};
	
	return config;
}
gdgs.ndp.gridbar.generateChartType = function(chartTypeData,plotData){
	var zoomType = plotData.get('zoomType');
	var inverted = plotData.get('inverted');
	if(zoomType){
		chartTypeData.set('zoomType',zoomType);
	}
	if(inverted){
		chartTypeData.set('inverted',inverted);
	}
	chartTypeData.set('zoomType',zoomType);
	chartTypeData.set('inverted',inverted);
	if(chartTypeData){
		var jblx = chartTypeData.get('jblx');
		var fColor = chartTypeData.get('fColor');
		var sColor = chartTypeData.get('sColor');
		var backgroundColor1 = gdgs.ndp.gridbar.valueColor(jblx,fColor,sColor);
		if(backgroundColor1){
			chartTypeData.set('backgroundColor',backgroundColor1);
		}
		
		var plotjblx = chartTypeData.get('plotjblx');
		var plotfColor = chartTypeData.get('plotfColor');
		var plotsColor = chartTypeData.get('plotsColor');
		var backgroundColor2 = gdgs.ndp.gridbar.valueColor(plotjblx,plotfColor,plotsColor);
		if(backgroundColor2){
			chartTypeData.set('plotBackgroundColor',backgroundColor2);
		}
	}
	
	return chartTypeData.toJSON();
}
gdgs.ndp.gridbar.generateColors =  function(colorsData){
	var colorsJSON=[];
	colorsData.each(function(k,v){
		var jblx = k.get('jblx');
		var fColor = k.get('fColor');
		var sColor = k.get('sColor');
		var singleColor;
		switch(jblx){
			case '0': 
				singleColor = fColor;
			break;
			case '1':
				singleColor = {
					linearGradient:{x1:0,x2:0,y1:0,y1:1},
					stops:[
						[0,fColor],
						[1,sColor]
					]
				};
			break;
			case '2':
				singleColor = {
				radialGradient:{cx:0.5,cy:0.5,r:0.5},
				stops:[
					[0,fColor],
					[1,sColor]
				]
			};
			break;
		}
		colorsJSON.push(singleColor);
	});
	return  colorsJSON;
}
gdgs.ndp.gridbar.generateLegend = function(legendData){
	if(legendData){
		var legendjblx = legendData.get('legendjblx');
		var legendfColor = legendData.get('legendfColor');
		var legendsColor = legendData.get('legendsColor');
		var backgroundColorlegend=gdgs.ndp.gridbar.valueColor(legendjblx,legendfColor,legendsColor);
		legendData.set('backgroundColor',backgroundColorlegend);
	}
	return legendData.toJSON();
}
gdgs.ndp.gridbar.generateTitle = function(titleData){
	return titleData.toJSON();
}
gdgs.ndp.gridbar.generateOptions = function(optionsData,chartTypeData,plotData,specialData,dataLabelsData){
	var grouping = plotData.get('grouping');
	var stacking = plotData.get('stacking');
	var colorByPoint = plotData.get('colorByPoint');
	var step = plotData.get('step');
	var innerSize = plotData.get('innerSize');
	var neckWidth = plotData.get('neckWidth');
	var medianColor = plotData.get('medianColor');
	var medianWidth = plotData.get('medianWidth');
	var stemColor = plotData.get('stemColor');
	var stemDashStyle = plotData.get('stemDashStyle');
	var stemWidth = plotData.get('stemWidth');
	var whiskerColor = plotData.get('whiskerColor');
	var whiskerLength = plotData.get('whiskerLength');
	var whiskerWidth = plotData.get('whiskerWidth');
	
	specialData.set('grouping',grouping);
	specialData.set('stacking',stacking==''||stacking=='undefined' ? null:stacking);
	specialData.set('colorByPoint',colorByPoint);
	specialData.set('step',step);
	specialData.set('innerSize',innerSize);
	specialData.set('neckWidth',neckWidth);
	specialData.set('medianColor',medianColor);
	specialData.set('medianWidth',medianWidth);
	specialData.set('stemColor',stemColor);
	specialData.set('stemDashStyle',stemDashStyle);
	specialData.set('stemWidth',stemWidth);
	specialData.set('whiskerColor',whiskerColor);
	specialData.set('whiskerLength',whiskerLength);
	specialData.set('whiskerWidth',whiskerWidth);

	var dataLabeljblx = dataLabelsData.get('dataLabeljblx');
	var dataLabelfColor = dataLabelsData.get('dataLabelfColor');
	var dataLabelsColor = dataLabelsData.get('dataLabelsColor');
	var backgroundColordataLabel =gdgs.ndp.gridbar.valueColor(dataLabeljblx,dataLabelfColor,dataLabelsColor);;
	dataLabelsData.set('backgroundColor',backgroundColordataLabel);
	specialData.set('dataLabels',dataLabelsData);
	optionsData.set(chartTypeData.get('type'),specialData);
	return optionsData.toJSON();
	
}
gdgs.ndp.gridbar.generateToolTip = function(toolTipData){
	var tooltipjblx = toolTipData.get('tooltipjblx');
	var tooltipfColor = toolTipData.get('tooltipfColor');
	var tooltipsColor = toolTipData.get('tooltipsColor');
	var backgroundColortooltip=gdgs.ndp.gridbar.valueColor(tooltipjblx,tooltipfColor,tooltipsColor);
	toolTipData.set('backgroundColor',backgroundColortooltip);
	return toolTipData.toJSON();
}
gdgs.ndp.gridbar.generateXData = function(xData){
	var alternateGridjblx = xData.get('alternateGridjblx');
	var alternateGridfColor = xData.get('alternateGridfColor');
	var alternateGridsColor = xData.get('alternateGridColor');
	var backgroundColoralternateGrid=gdgs.ndp.gridbar.valueColor(alternateGridjblx,alternateGridfColor,alternateGridsColor);;
	xData.set('alternateGridColor',backgroundColoralternateGrid);
	
	var titleText = xData.get('title');
	var xDataJSON = xData.toJSON();
	xDataJSON["title"] = {
		"text" : titleText
	};
	return xDataJSON;
}
gdgs.ndp.gridbar.generateYData = function(yData){
	var alternateGridjblx = yData.get('alternateGridjblx');
	var alternateGridfColor = yData.get('alternateGridfColor');
	var alternateGridsColor = yData.get('alternateGridColor');
	var backgroundColoralternateGrid=gdgs.ndp.gridbar.valueColor(alternateGridjblx,alternateGridfColor,alternateGridsColor);;
	yData.set('alternateGridColor',backgroundColoralternateGrid);
	var titleText = yData.get('title');
	var yDataJSON = yData.toJSON();
	yDataJSON["title"] = {
		"text" : titleText
	};
	return yDataJSON;
}